<?php

namespace app\shopapi\controller;


use app\admin\model\Shop;
use app\admin\model\ShopMessage;
use app\api\model\ShopSettlement;
use app\common\controller\Shopapi;
use app\common\model\ShopMoneyLog;
use app\shopapi\model\Feedback;
use think\Request;


/**
 * 店铺管理接口
 */
class Shopmanage extends Shopapi
{

    protected $noNeedRight = '*';

    protected $shop_id;

    protected $shop_name;

    protected $shop_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        /*$shop_info = \app\admin\model\Shop::alias('s')
            ->join('shop_category sc', 'sc.id=s.category_id', 'LEFT')
            ->where(['s.user_id'=>$this->auth->id])
            ->field('s.*,sc.name as category_name')->find();

       */
        $shop_info = Shop::get(['username'=>$this->shopauth->username]);
        if (empty($shop_info)){
            $this->error('当前用户不是店铺管理员');
        }
        $this->shop_info = $shop_info;
        $this->shop_id = $shop_info->id;
        $this->shop_name = $shop_info->shop_name;
    }

    //店铺信息
    public function info()
    {
        $this->success('',$this->shop_info);
    }


    //消息列表
    public function message()
    {
        $list = \app\admin\model\ShopMessage::where(['shop_id'=>$this->shop_id])
            ->order('createtime desc')
            ->paginate(10);
        $this->success('',$list);
    }

    //消息详情
    public function messageDetail()
    {
        $id = input('id',0);
        $detail = ShopMessage::get($id);
        $this->success('',$detail);
    }

    //意见反馈
    public function feedback()
    {
        $content = input('content','');

        if (empty($content))$this->error('请输入反馈内容');

        $model = new Feedback();
        $data = [
            'shop_id'=>$this->shop_id,
            'content'=>$content,
        ];

        $model->save($data);

        $this->success('反馈成功');
    }

    /**
     * 余额日志
     * @return string
     */
    public function moneylog()
    {
        $where = ['l.shop_id'=>$this->shop_id];
        $status = input('status','all');
        $date = input('date','');
        if ($status==1){
            $where['l.money'] = ['>',0];
        } elseif ($status==2){
            $where['l.money'] = ['<',0];
        }
        if ($date){
            $where['l.createtime'] = ['between', [strtotime($date), strtotime($date)+86399]];
        }

        $moneyloglist = ShopMoneyLog::alias('l')
            ->join('user u', 'u.id=l.user_id', 'LEFT')
            ->where($where)
            ->field('l.*,u.avatar,u.nickname,u.mobile')
            ->order('l.createtime desc')
            ->paginate(10);
        foreach ($moneyloglist as $k=>$v){
            $moneyloglist[$k]['mobile'] = maskPhoneNumberRegex($v['mobile']);
        }

        $this->success('',$moneyloglist);
    }

    //已结算/未结算金额
    public function money()
    {
        $settlement_money = ShopSettlement::where(['shop_id'=>$this->shop_id,'is_settlement'=>1])->sum('shop_money');
        $wait_settlement_money = ShopSettlement::where(['shop_id'=>$this->shop_id,'is_settlement'=>0])->sum('shop_money');
        $data = [
            'settlement_money'  =>$settlement_money,
            'wait_settlement_money'=>$wait_settlement_money
        ];
        $this->success('',$data);

    }


    //生成付款码
    public function buildQrcode()
    {
        $config = get_addon_config('qrcode');

        $params = $this->request->get();
        $params = array_intersect_key($params, array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelmargin', 'labelfontsize', 'labelalignment', 'labelfontcolor']));

        //$params['text'] = $this->request->get('text', $config['text'], 'trim');
        //$params['label'] = $this->request->get('label', $config['label'], 'trim');
        //$params['user_id']=$this->auth->id;
        $request = Request::instance();
        $domain=$request->domain();

        $params['text'] = $domain.'/shop?shop_id='.$this->shop_id;
        $qrCode = \addons\qrcode\library\Service::qrcode($params);


        $qrcodePath = ROOT_PATH . 'public/uploads/shopqrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if (is_really_writable($qrcodePath)) {
            //$filePath = $qrcodePath . md5(implode('', $params)) . '.' . $config['format'];
            $filePath = $qrcodePath  . 'collectmoney_shop_id_'.$this->shop_id . '.' . $config['format'];
            $qrCode->saveToFile($filePath);
        }
        $path =  str_replace(ROOT_PATH.'public', "", $filePath);
        $this->success('',$path);

    }

}